/*********************************************************************************
 *      Copyright:  (C) 2025 ZYT
 *                  All rights reserved.
 *
 *       Filename:  thread1.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(10/08/25)
 *         Author:  ZYT <zhangyuting@163.com>
 *      ChangeLog:  1, Release initial version on "10/08/25 09:31:10"
 *                 
 ********************************************************************************/
#include "main.h"
#include "bridge.h"
#include <stdio.h>
#include <unistd.h>

void* thread1_func(void* arg) 
{
	struct can_frame tx_frame, rx_frame;
	int				 i;

	// 构造一个 J1939 帧发送
	tx_frame.can_id = j1939_can_id(6, J1939_PGN(0x100), 0x80) | CAN_EFF_FLAG;
	tx_frame.can_dlc = 8;
	for (i = 0; i < 8; i++) 
		tx_frame.data[i] = i;

	while (1) 
	{
		// 发送
		if (thread1_to_bridge(&tx_frame) == 0) 
		{
			printf("[Thread1] Sent CAN frame.\n");
		}

		// 接收
		if (bridge_to_thread1(&rx_frame) == 0) 
		{
			//printf("[Thread1] Received: ID=0x%08X DLC=%d\n", rx_frame.can_id, rx_frame.can_dlc);
			printf("[Thread1] Got CAN: ID=0x%08X  DLC=%d  ", rx_frame.can_id, rx_frame.can_dlc);
			for (i = 0; i < rx_frame.can_dlc; i++) 
			{
				printf("Data[%d]=%02X ", i, rx_frame.data[i]);
			}
			printf("\n");
		}

		sleep(1);
	}

	return NULL;
}
